package com.droid.library.widget.progress;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Build;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.xinye.lib.R;

public class CircleProgressView extends View {

    private static final String TAG = "CircleProgressBar";

    private int mBgColor = Color.WHITE;
    private int mProgressColor = Color.BLACK;
    private int mStrokeWidth = 8;
    private int mMax = 100;
    private int mProgress = 0;

    // 画圆所在的距形区域
    private RectF mRectF;

    private Paint mPaint;

    public CircleProgressView(Context context) {
        super(context);
        init(context,null);
    }

    public CircleProgressView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context,attrs);
    }

    public CircleProgressView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context,attrs);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public CircleProgressView(Context context, @Nullable AttributeSet attrs,
                              int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context,attrs);
    }

    private void init(Context context, AttributeSet attrs){

        if(context != null && attrs != null){
            TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CircleProgressView);
            mBgColor = ta.getColor(R.styleable.CircleProgressView_cpbBgColor,mBgColor);
            mProgressColor = ta.getColor(R.styleable.CircleProgressView_cpbProgressColor,mProgressColor);
            mStrokeWidth = ta.getDimensionPixelOffset(R.styleable.CircleProgressView_cpbWidth,mStrokeWidth);
            mMax = ta.getInt(R.styleable.CircleProgressView_cpvMax,mMax);
            mProgress = ta.getInt(R.styleable.CircleProgressView_cpvProgress,mProgress);
            ta.recycle();
        }

        mRectF = new RectF();
        mPaint = new Paint();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int width = this.getWidth();
        int height = this.getHeight();

        if (width != height) {
            int min = Math.min(width, height);
            width = min;
            height = min;
        }

        // 设置画笔相关属性
        mPaint.setAntiAlias(true);
        mPaint.setStrokeWidth(mStrokeWidth);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(mBgColor);
        // 位置
        mRectF.left = mStrokeWidth / 2; // 左上角x
        mRectF.top = mStrokeWidth / 2; // 左上角y
        mRectF.right = width - mStrokeWidth / 2; // 左下角x
        mRectF.bottom = height - mStrokeWidth / 2; // 右下角y

        // 绘制圆圈，进度条背景
        canvas.drawArc(mRectF, -90, 360, false, mPaint);
        mPaint.setColor(mProgressColor);
        canvas.drawArc(mRectF, -90, ((float) mProgress / mMax) * 360, false, mPaint);
    }

    public void setProgress(int progress) {
        mProgress = progress;
        invalidate();
    }

    public void setMax(int max){
        mMax = max;
        postInvalidate();
    }

    public void setBgColor(int color){
        mBgColor = color;
        postInvalidate();
    }
    public void setProgressColor(int color){
        mProgressColor = color;
        postInvalidate();
    }

}